package com.wss.module.main.ui.page.flip.adapter;

import com.wss.common.base.adapter.BaseListAdapter;
import com.wss.common.base.adapter.superadapter.SuperViewHolder;
import com.wss.common.utils.ColorUtils;
import com.wss.common.utils.ImageUtils;
import com.wss.module.main.ResourceTable;
import com.wss.module.main.ui.page.flip.helper.ImagePiece;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Describe：适配器
 * Created by 吴天强 on 2021/2/8.
 */
public class ImageAdapter extends BaseListAdapter<ImagePiece> {

    /**
     * 点击事件
     */
    private OnClickListener clickListener;
    /**
     * 是否是某曾
     */
    private boolean isMantle;

    /**
     * 常规列表重写该方法
     *
     * @param context     context
     * @param list        数据源
     * @param clickListener    Item点击回调
     */
    public ImageAdapter(Context context,
                        List<ImagePiece> list,
                        OnClickListener clickListener) {
        super(context, list, ResourceTable.Layout_main_item_of_card_flip, null);
        this.clickListener = clickListener;
    }

    @Override
    public void onBindData(@NotNull SuperViewHolder holder,
                           int layoutPosition,
                           @NotNull ImagePiece data) {
        Image imageView = holder.findViewById(ResourceTable.Id_image);
        if (isMantle) {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(ColorUtils.colorToRgbColor(ResourceTable.Color_theme));
            imageView.setBackground(element);
            imageView.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    clickListener.onClick(layoutPosition, imageView);
                }
            });
        } else {
            ImageUtils.loadImage(imageView, data.getBitmap());
            if (data.isAnim()) {
                startAnim(imageView);
                //开始动画了就把属性设置为false 防止下次翻转动画的时候又执行一遍
                data.setAnim(false);
            }
        }
    }

    public void setMantle(boolean mantle) {
        isMantle = mantle;
    }

    public interface OnClickListener {
        void onClick(int position, Image imageView);
    }

    private void startAnim(Image image) {
        AnimatorGroup animatorGroup = new AnimatorGroup();
        AnimatorProperty disappear = new AnimatorProperty();
        disappear.setTarget(image);
        disappear.alphaFrom(1.0f).alpha(0f);
        AnimatorProperty rotation = new AnimatorProperty();
        rotation.setDuration(1000)
                .rotate(-180);
        AnimatorProperty appear = new AnimatorProperty();
        appear.setDelay(500)
                .alphaFrom(0f)
                .alpha(1f);
        animatorGroup.runParallel(disappear, rotation, appear);
        animatorGroup.start();
    }
}
